package com.qualityevaluationclouduser.controller;

import com.qualityevaluationclouduser.entity.MenuInfo;
import com.qualityevaluationclouduser.service.MenuInfoService;
import com.qualityevaluationclouduser.untils.ResultEntity;
import com.qualityevaluationclouduser.untils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: qualityEvaluationCloudProject
 * @description: 权限信息控制类
 * @author:
 * @create:
 **/
@RestController
@RequestMapping("/authority")
@Api(tags = "菜单信息控制器")
public class MenuController {

    @Autowired
    private MenuInfoService menuInfoService;

    @PostMapping("/add")
    @ApiOperation(value = "新增权限", notes = "新增权限")
//    @PreAuthorize("hasAnyAuthority('ROLE_USER')")
    public ResultEntity addAuthority(@RequestBody MenuInfo menuInfo) {
        MenuInfo result = menuInfoService.addMenuInfo(menuInfo);
        return ResultUtils.build(result);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除权限信息", notes = "删除权限信息")
//    @PreAuthorize("hasAnyAuthority('ROLE_USER')")
    public void delAuthority(@PathVariable("id") String id) {
        menuInfoService.delMenuInfo(id);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新权限信息", notes = "更新权限信息")
//    @PreAuthorize("hasAnyAuthority('ROLE_USER')")
    public ResultEntity updMenuInfo(@RequestBody MenuInfo menuInfo) {
        MenuInfo result = menuInfoService.updMenuInfo(menuInfo);
        return ResultUtils.build(result);
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询所有权限信息", notes = "查询所有权限信息")
//    @PreAuthorize("hasAnyAuthority('ROLE_USER')")
    public ResultEntity findMenuInfoList() {
        return ResultUtils.build(menuInfoService.findMenuInfoList());
    }

    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping("/batch")
    public void batchDelete(@RequestParam String idString) {
        String[] split = idString.split(",");
        List<Long> ids = new ArrayList<>();
        for (String id : split
        ) {
            ids.add(Long.parseLong(id));
        }
        menuInfoService.deleteMenuInfosByIdIn(ids);
    }

    @ApiOperation(value = "分页查询权限信息", notes = "分页查询权限信息")
    @GetMapping("/page")
    public ResultEntity pageQueryMenuInfoList(@RequestParam int page, @RequestParam int size) {
        return ResultUtils.build(menuInfoService.pageQueryMenuInfoList(page,size));
    }

    @ApiOperation(value = "根据名称分页查询权限信息",notes = "根据名称分页查询权限信息")
    @GetMapping("/specific")
    public ResultEntity pageQueryMenuInfoByName(String name,@RequestParam int page,@RequestParam int size){
        return ResultUtils.build(menuInfoService.pageQueryMenuInfoByName(name,page,size));
    }
}
